<?php
/**
 * Note: 不同渠道不同方案每日消费
 */

namespace App\Models\Mongo\DailyLog;

use App\Traits\ModelTrait;
use App\Traits\MongoModelTrait;
use Jenssegers\Mongodb\Collection;

class DayCpaCost extends Base
{
    use ModelTrait, MongoModelTrait;

    protected $table = 'dayCpaCost';

    protected $fillable = [
        'day',
        'source',
        'channel',
        'advertiserId',
        'campaignId',
        'campaignName',
        'projectId',
        'projectName',
        'cpaCost',
        'cpaCostActual',
        'rebate',
        // 前端数据
        'active',// 激活
        'register',// 注册
        'payFirstUser',// 新增付费人数
        'payCnt',// 付费次数
        'pay',// 付费金额
        'activeDayPay',// 激活当日付费金额
        'active24HourPay',// 激活24H付费金额
    ];

    public const CALC_WITHOUT_FIELDS = ['day', 'source', 'channel', 'advertiserId', 'campaignId', 'campaignName', 'projectId', 'projectName', 'rebate',];

    protected $casts = [
        'day' => 'string',
        'source' => 'integer',
        'channel' => 'string',
        'advertiserId' => 'integer',
        'campaignId' => 'integer',
        'campaignName' => 'string',
        'projectId' => 'integer',
        'projectName' => 'string',
        'cpaCost' => 'integer',
        'cpaCostActual' => 'integer',
        'active' => 'integer',
        'register' => 'integer',
        'payFirstUser' => 'integer',
        'payCnt' => 'integer',
        'pay' => 'integer',
        'activeDayPay' => 'integer',
        'active24HourPay' => 'integer',
        // 'rebate' => 'integer',
    ];

    /**
     * 获取实际消费 根据渠道 [$source1 => $cost1, $source2 => $cost2]
     * @param array $dateRange
     * @return array
     */
    public function getCpaCostActuals(array $dateRange = []): array
    {
        $groups = [
            '_id' => '$source',
            'cpaCostActual' => [
                '$sum' => '$cpaCostActual',
            ]
        ];
        $aggregate = [
            [
                '$match' => [
                    'day' => [
                        '$gte' => $dateRange[0],
                        '$lte' => $dateRange[1],
                    ],
                    // 'source' => $source,
                ],
            ],
            [
                '$group' => $groups,
            ],
        ];
        $res = self::raw(function (Collection $collection) use ($aggregate) {
            return $collection->aggregate($aggregate);
        });
        if (!$res->count()) return [];

        return $res->pluck('cpaCostActual', '_id')->all();
    }

    /**
     * @param array $source
     * @param string $day
     * @return array
     */
    public function getDataBySourceDay(array $source = [], string $day = ''): array
    {
        $fields = array_filter($this->fillable, function($field) {
            return !in_array($field, self::CALC_WITHOUT_FIELDS);
        });

        $groups = [
            '_id' => '$source',
            'source' => [
                '$last' => '$source'
            ],
        ];

        foreach ($fields as $field) {
            $groups[$field] = [
                '$sum' => '$'.$field,
            ];
        }
        $match = [
            'day' => $day,
            'source' => [
                '$ne' => 0,
            ],
        ];

        if ($source) {
            $match['source'] = [
                '$in' => array_map('intval', $source),
            ];
        }
        $aggregate = [
            [
                '$match' => $match,
            ],
            [
                '$group' => $groups,
            ],
            [
                '$sort' => [
                    '_id' => 1,
                ],
            ],
        ];
        $res = self::raw(function (Collection $collection) use ($aggregate) {
            return $collection->aggregate($aggregate);
        });
        if (!$res->count()) return [];

        // $data = $res->toArray();
        $lists = [];
        foreach ($res as $v) {
            $lists[$v['source']] = $v;
        }
        return $lists;
    }
}
